package com.fcat.common.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.fcat.springboot.core.util.SpringbootUtil;
import com.fcat.springboot.mybatis.controller.TsuantoneBaseController;
import com.fcat.springboot.mybatis.util.JsonUtil;
import com.fcat.base.common.util.StrUtil;
import com.fcat.common.user.entity.Organization;
import com.fcat.common.user.service.OrganizationService;
import com.fcat.common.user.vo.OrganizationVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("organization")
public class OrganizationController extends TsuantoneBaseController<OrganizationService, Organization, Long> {


    @ApiOperation("获取组织架构树形结构")
    @GetMapping("getOrganizationTree")
    public JSONObject getOrganizationTree() {
        String username = SpringbootUtil.parseToken().getUsername();
        if (StrUtil.isBlank(username)) return JsonUtil.getFailJsonObject("请先进行登录");
        return bsi.getTreeByUsername(username);
    }


    @ApiOperation("编辑组织")
    @PostMapping("/updateOrganization")
    public JSONObject updateOrganization(@RequestBody Organization organization) {
        if (organization == null) return JsonUtil.getFailJsonObject("请选择角色信息");
        organization.setUpdateTime(new Date());
        Integer num = bsi.updateSelectiveById(organization);
        if (num == 1) return JsonUtil.getSuccessJsonObject();
        return JsonUtil.getFailJsonObject("修改失败");
    }

    @ApiOperation("添加下级组织")
    @PostMapping("/addSubordinateOrganization")
    public JSONObject addSubordinateOrganization(@RequestBody Organization organization) {
        String username = SpringbootUtil.parseToken().getUsername();
        Long companyId = SpringbootUtil.parseToken().getCompanyId();
        if (organization == null) return JsonUtil.getFailJsonObject("请填写完整的组织信息");
        if (organization.getParentId() == null) return JsonUtil.getFailJsonObject("请选择上级组织");
        organization.setUsername(username);
        organization.setCompanyId(companyId);
        Integer num = bsi.insert(organization);
        if (num == 1) return JsonUtil.getSuccessJsonObject("添加成功");
        return JsonUtil.getSuccessJsonObject("添加失败");
    }


    @ApiOperation("获取id位置打开的树")
    @GetMapping("/deploymentTree")
    public JSONObject deploymentTree(@RequestParam("id") Long id) {
        if (id == null) {
            id = 0L;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        List<OrganizationVO> organizationVOList = bsi.getOrganizationTree(map);
        return JsonUtil.getSuccessJsonObject(organizationVOList);
    }

    @ApiOperation("组织架构搜索")
    @GetMapping("search")
    public JSONObject search(String keyword) {
        String username = SpringbootUtil.parseToken().getUsername();
        Long companyId = SpringbootUtil.parseToken().getCompanyId();
        return JsonUtil.getSuccessJsonObject(bsi.search(keyword,companyId,username));
    }

    @ApiOperation("组织架构搜索")
    @GetMapping("searchByIdAndType")
    public JSONObject searchByIdAndType(@RequestParam(value = "id",required = true)Long id,
                             @RequestParam(value = "type",required = true) String type ) {
        String username = SpringbootUtil.parseToken().getUsername();
        Long companyId = SpringbootUtil.parseToken().getCompanyId();
        return JsonUtil.getSuccessJsonObject(bsi.searchByIdAndType(id,type,companyId,username));
    }

    @ApiOperation("查询当前单位下面的组织")
    @GetMapping("getTreeByCompanyId")
    public JSONObject getTreeByCompanyId(){
        String username = SpringbootUtil.parseToken().getUsername();
        Long companyId = SpringbootUtil.parseToken().getCompanyId();
        List<OrganizationVO> organizationVOList=bsi.getTreeByCompanyId(companyId,username);
        return JsonUtil.getSuccessJsonObject(organizationVOList);
    }

}